﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#pragma once

// std headers
#include <memory>
#include <map>

// Qt headers
#include <QWidget>
#include <QMap>

// custom headers
#include "UrlImageProvider.h"


class QImage;
class QSize;
class QFrame;
class QSignalMapper;
class QHBoxLayout;
class QTimer;

namespace gmepgcs
{
    class ImageSlider : public QWidget
    {
        Q_OBJECT
    public:
        explicit ImageSlider(QWidget *parent = nullptr);
        virtual ~ImageSlider();

        void SetImages(std::vector<std::wstring> imageKeys, std::map<std::wstring, std::wstring> titles = {});
        //void SetImageProvider(std::shared_ptr<UrlImageProvider> imageProvider);// 暂时注释掉未使用的函数

        void ResetImages();

    public:
        void SetImageFrameSize(const QSize &frameSize);
        void SetShowTitle(const bool &visible);
        void SetShowBorder(const bool &showBorder);
        void SetStretch(const bool &stretch);
        //void SetLoop(const bool &loop); // 暂时注释掉未使用的函数
        //void SetAutoPlay(const bool &autoPlay);// 暂时注释掉未使用的函数

    public slots:
        void SlideTo(int32_t index);
        void SlideBack();
        void SlideForward();        

    private:
        void Init();
        void CheckDots();

    private slots:
        void UpdateImage(std::wstring imageKey, std::shared_ptr<QImage> spImage);

    private:
        std::vector<std::wstring> m_imageKeys;
        std::map<std::wstring, std::wstring> m_mpImageTitles;
        int32_t m_currentIndex;
        int32_t m_previousIndex;
        QMap<std::wstring, std::shared_ptr<QImage>> m_mpImages;
        bool m_stretch;
        bool m_bLoop;
        bool m_bAutoPlay;

    private:
        std::shared_ptr<UrlImageProvider> m_spImageProvider;
        QFrame *m_pImageFrame;
        QLabel *m_pLblImage;
        QLabel *m_pLblTitle;
        QSignalMapper *m_pDotsSigalMapper;
        std::map<int, std::shared_ptr<QPushButton>> m_mpBtnDots;
        QHBoxLayout *m_pDotsLayout;
        QTimer *m_pAutoPlayTimer;
    };
}